import { type ApiOrder, ORDERSTATUS, PACKAGESTATUS, QUERYORDERSTATUS, SHOPITEMSTATUS, type ShopOrder, SHOPORDERSTATUS } from '@/pages/order/types'
// import { getAfsStatusCn } from '@/hooks'

export const UPLOAD_URL = import.meta.env.VITE_BASE_URL + import.meta.env.VITE_UPLOAD_URI
type OptionalStatus = keyof typeof ORDERSTATUS | keyof typeof SHOPORDERSTATUS | keyof typeof PACKAGESTATUS
// 订单详情图标配置
export const orderDetailIconConfig = {
  等待买家付款: 'icon-zhangdan',
  待发货: 'icon-daifahuo',
  待收货: 'icon-ziyuan',
  交易成功: 'icon-jiaoyichenggong',
  待评价: 'icon-jiaoyichenggong',
  交易关闭: 'icon-jiaoyichenggong',
  系统关闭订单: 'icon-dingdanguanbi-copy',
  已完成: 'icon-jiaoyichenggong',
} as const
const queryStatus: Record<keyof typeof QUERYORDERSTATUS, string> = {
  UN_DELIVERY: '待发货(待备货)',
  // UNPAID: '待支付',
  UN_RECEIVE: '待收货',
  COMPLETED: '已完成',
  UN_COMMENT: '待评价',
  CLOSED: '已关闭',
}
const orderStatus: Record<keyof typeof ORDERSTATUS, string> = {
  UNPAID: '等待买家付款',
  PAID: '已支付',
  BUYER_CLOSED: '买家关闭订单',
  SYSTEM_CLOSED: '系统关闭订单',
  SELLER_CLOSED: '卖家关闭订单',
}
const shopOrderStatus: Record<keyof typeof SHOPORDERSTATUS, string> = {
  OK: '正常状态',
  SYSTEM_CLOSED: '系统关闭',
  BUYER_CLOSED: '买家关闭订单',
  SELLER_CLOSED: '卖家关闭订单',
}
const shopItemsStatus: Record<keyof typeof SHOPITEMSTATUS, string> = {
  OK: '确认',
  CLOSED: '订单已关闭',
}
const packageStatus: Record<keyof typeof PACKAGESTATUS, string> = {
  WAITING_FOR_DELIVER: '待发货',
  WAITING_FOR_RECEIVE: '待收货',
  BUYER_WAITING_FOR_COMMENT: '待评价',
  SYSTEM_WAITING_FOR_COMMENT: '待评价',
  BUYER_COMMENTED_COMPLETED: '已完成',
  SYSTEM_COMMENTED_COMPLETED: '已完成',
}
interface OrderDetailsConfig {
  isShowAfsBtn: boolean
  isShowFooterBtn: boolean
  isShowFooterRightBtnText: string
  isShowModifyAddressBtn: boolean
}
/**
 * 订单详情状态页面配置
 */
export const getOrderDetailsConfig = (order: ApiOrder) => {
  const orderDetailsConfig = {
    isShowAfsBtn: false,
    isShowFooterBtn: true,
    isShowFooterRightBtnText: '',
    isShowModifyAddressBtn: false,
  }
  if (order.status === 'UNPAID') return unpaid(orderDetailsConfig)
  if (order.shopOrders[0].status === 'OK') return shopOrdersOk(order.shopOrders[0], orderDetailsConfig)
  return orderDetailsConfig
}
const unpaid = (orderDetailsConfig: OrderDetailsConfig) => {
  orderDetailsConfig.isShowAfsBtn = false
  orderDetailsConfig.isShowFooterRightBtnText = '立即付款'
  return orderDetailsConfig
}
const shopOrdersOk = (shopOrders: ShopOrder, orderDetailsConfig: OrderDetailsConfig) => {
  const okNum = shopOrders.shopOrderItems.filter((orderItem) => orderItem.status === 'OK').length
  if (okNum === shopOrders.shopOrderItems.length) {
    // 所有的订单状态都是'OK' 查看包状态
    return pagesStatus(shopOrders, orderDetailsConfig)
  }
  // 订单状态不一致分开处理
  handledSeparately(shopOrders, orderDetailsConfig)
  return orderDetailsConfig
}
// * @param NONE 无售后
// * @param REFUND_REQUEST 申请退款
// * @param SYSTEM_REFUND_AGREE 系统自动同意退款申请
// * @param REFUND_AGREE 已同意退款申请
// * @param REFUND_REJECT 拒绝了退款申请
// * @param REFUNDED 退款已到账
// * @param RETURN_REFUND_REQUEST 申请退货退款
// * @param SYSTEM_RETURN_REFUND_AGREE 系统自动同意退货退款申请
// * @param RETURN_REFUND_AGREE 已同意退货退款申请
// * @param RETURN_REFUND_REJECT 拒绝了退货退款申请
// * @param SYSTEM_RETURNED_REFUND_CONFIRM 退货退款 系统自动确认收货
// * @param RETURNED_REFUND   退货已发出
// * @param SYSTEM_CLOSED 系统自动关闭
// * @param RETURNED_REFUND_CONFIRM 确认退货已收到
// * @param RETURNED_REFUND_REJECT 已拒绝收货
// * @param  RETURNED_REFUNDED 退货退款已完成
// * @param  BUYER_CLOSED  主动撤销了售后申请
const handledSeparately = (shopOrders: ShopOrder, orderDetailsConfig: OrderDetailsConfig) => {
  // 此订单处于关闭状态 说明退款了 需要展示退款成功按钮
  orderDetailsConfig.isShowAfsBtn = true
}
const pagesStatus = (shopOrders: ShopOrder, orderDetailsConfig: OrderDetailsConfig) => {
  const len = shopOrders.shopOrderItems.length
  // sendGoodsNum 找出待发货的订单个数
  const sendGoodsNum = shopOrders.shopOrderItems.filter((orderItem) => orderItem.packageStatus === 'WAITING_FOR_DELIVER').length
  if (sendGoodsNum === len) {
    orderDetailsConfig.isShowModifyAddressBtn = true
    orderDetailsConfig.isShowAfsBtn = true
    orderDetailsConfig.isShowFooterRightBtnText = ''
    return orderDetailsConfig
  }

  return sendGoodsNumNext(shopOrders, orderDetailsConfig)
}
// 已发货
const sendGoodsNumNext = (shopOrders: ShopOrder, orderDetailsConfig: OrderDetailsConfig) => {
  const confirmGoods = shopOrders.shopOrderItems.some((orderItem) => orderItem.packageStatus === 'WAITING_FOR_RECEIVE')

  if (confirmGoods) {
    orderDetailsConfig.isShowAfsBtn = true
    orderDetailsConfig.isShowFooterRightBtnText = '确认收货'
    return orderDetailsConfig
  }
  const comment = shopOrders.shopOrderItems.some((orderItem) => orderItem.packageStatus === 'BUYER_WAITING_FOR_COMMENT')
  const commentSystem = shopOrders.shopOrderItems.some((orderItem) => orderItem.packageStatus === 'SYSTEM_WAITING_FOR_COMMENT')
  if (comment || commentSystem) {
    orderDetailsConfig.isShowFooterRightBtnText = '评价'
    orderDetailsConfig.isShowAfsBtn = true
    return orderDetailsConfig
  }
  // 已完成
  return completed(shopOrders, orderDetailsConfig)
}
const completed = (shopOrders: ShopOrder, orderDetailsConfig: OrderDetailsConfig) => {
  const isCompleted = shopOrders.shopOrderItems.some((orderItem) => orderItem.packageStatus === 'BUYER_COMMENTED_COMPLETED')
  const isCompletedSystem = shopOrders.shopOrderItems.some((orderItem) => orderItem.packageStatus === 'SYSTEM_COMMENTED_COMPLETED')
  if (isCompleted || isCompletedSystem) {
    orderDetailsConfig.isShowFooterBtn = false
    orderDetailsConfig.isShowFooterRightBtnText = ''
    orderDetailsConfig.isShowAfsBtn = false
    return orderDetailsConfig
  }

  orderDetailsConfig.isShowFooterBtn = false
  orderDetailsConfig.isShowFooterRightBtnText = ''
  orderDetailsConfig.isShowAfsBtn = false
  return orderDetailsConfig
}
// export const orderListFooterBtnText = (shopOrderItems: ShopOrderItem[]) => {
//     const footerBtnText = {
//         isShowBtns: false,
//         leftText: '',
//         rightText: '',
//         deliverType: shopOrderItems && shopOrderItems.length > 0 ? shopOrderItems[0].deliverType : '',
//     }
//     // 待收货
//     const delivery = shopOrderItems.filter((order) => order.packageStatus === 'WAITING_FOR_RECEIVE').length
//     const refundedNum = shopOrderItems.filter((order) => getAfsStatusCn(order.afsStatus).isRefunded).length
//     if (refundedNum === shopOrderItems.length) {
//         footerBtnText.isShowBtns = false
//         return footerBtnText
//     }
//     //是否是无需物流发货
//     if (delivery) {
//         footerBtnText.isShowBtns = true
//         footerBtnText.leftText = '查看物流'
//         footerBtnText.rightText = '确认收货'
//         return footerBtnText
//     }
//     const comment = shopOrderItems.filter((order) => order.packageStatus === 'BUYER_WAITING_FOR_COMMENT').length
//     if (comment) {
//         footerBtnText.isShowBtns = true
//         footerBtnText.leftText = '查看物流'
//         footerBtnText.rightText = '评价'
//         return footerBtnText
//     }
//     return footerBtnText
// }
/**
 * 已付款订单状态改中文
 */
export const getOrderStatusZh = (order: ShopOrder) => {
  let newArr = 0
  // 待收货
  let delivery = 0
  if (order.status !== 'OK') return shopOrderStatus[order.status]
  newArr = order.shopOrderItems.filter((itme) => itme.status !== 'OK').length
  delivery = order.shopOrderItems.filter((item) => item.packageStatus === 'WAITING_FOR_RECEIVE').length
  if (newArr === order.shopOrderItems.length) {
    return '交易失败'
  }
  if (newArr + delivery === order.shopOrderItems.length) {
    return '待收货'
  }
  return delivery === 0 ? packageStatus[order.shopOrderItems[0].packageStatus] : '部分待收货'
}
/**
 * 如果是等待付款就展示付款按钮
 * @param {OptionalStatus} status
 */
export const isUnpaidOrder = (status: OptionalStatus) => {
  return ['UNPAID'].includes(status)
}
/**
 * 包裹状态改中文
 * @param {keyof} str
 */
const getPackagecn = (str: keyof typeof PACKAGESTATUS) => {
  return packageStatus[str]
}
/**
 * 未付款订单状态改中文
 * @param {keyof} str
 */

const getOrdercn = (str: keyof typeof ORDERSTATUS) => {
  return orderStatus[str]
}
const getShopcn = (str: keyof typeof SHOPORDERSTATUS) => {
  return shopOrderStatus[str]
}

/**
 * 是否支付订单
 * @param {OptionalStatus} status 订单下所有状态
 */
const isPayOrder = (status: OptionalStatus) => {
  return !['UNPAID'].includes(status)
}
const isCloseOrder = (status: OptionalStatus) => {
  return !['BUYER_CLOSED', 'SYSTEM_CLOSED'].includes(status)
}

export { queryStatus, orderStatus, shopOrderStatus, packageStatus, shopItemsStatus, getOrdercn, getShopcn, getPackagecn, isPayOrder, isCloseOrder }

/**
 * 订单状态 超级版
 */
export interface OrderStatusPlus {
  //订单状态 描述
  desc: string
  //是否已关闭
  isClosed: boolean
  //关闭时间
  closeTime?: string
}

/**
 * 根据订单详情 获取订单状态
 */
export const getOrderDetailStatusPlus = (order: ApiOrder | undefined): OrderStatusPlus => {
  if (!order) return { desc: '', isClosed: false }
  //未支付只需要检查总订单状态
  if (order.status !== 'PAID') {
    console.log(order.status)

    const statusPlus = OrderStatusPlusHandler[order.status]
    statusPlus.closeTime = order.updateTime
    return statusPlus
  }
  //已支付 订单已拆分显示 只需要取第一条店铺订单数据
  const shopOrder = order.shopOrders[0]
  if (shopOrder.status !== 'OK') {
    const statusPlus = ShopOrderStatusPlusHandler[shopOrder.status]
    statusPlus.closeTime = shopOrder.updateTime
    return statusPlus
  }
  const shopOrderItems = shopOrder.shopOrderItems
  const statusPlus: OrderStatusPlus = {
    desc: '交易失败',
    isClosed: true,
  }
  //检查是否全部关闭
  for (let shopOrderItem of shopOrderItems) {
    if (shopOrderItem.status === 'OK') {
      statusPlus.desc = packageStatus[shopOrderItem.packageStatus]
      statusPlus.isClosed = false
      return statusPlus
    }

    const currentCloseTime = statusPlus.closeTime
    const closeTime = shopOrderItem.updateTime
    if (!closeTime) {
      continue
    }
    statusPlus.closeTime = !currentCloseTime ? closeTime : closeTime > currentCloseTime ? closeTime : currentCloseTime
  }
  return statusPlus
}

/**
 *  OK: '正常状态',
 *     SYSTEM_CLOSED: '系统关闭',
 *     BUYER_CLOSED: '买家关闭订单',
 *     SELLER_CLOSED: '卖家关闭订单',
 */
export const ShopOrderStatusPlusHandler: Record<keyof typeof SHOPORDERSTATUS, OrderStatusPlus> = {
  OK: {
    desc: '正常状态',
    isClosed: false,
  },
  SYSTEM_CLOSED: {
    desc: '系统关闭',
    isClosed: true,
  },
  BUYER_CLOSED: {
    desc: '买家关闭订单',
    isClosed: true,
  },
  SELLER_CLOSED: {
    desc: '卖家关闭订单',
    isClosed: true,
  },
}
/**
 * UNPAID: '等待买家付款',
 * PAID: '已支付',
 * BUYER_CLOSED: '买家关闭订单',
 * SYSTEM_CLOSED: '系统关闭订单',
 * SELLER_CLOSED: '卖家关闭订单',
 */
export const OrderStatusPlusHandler: Record<keyof typeof ORDERSTATUS, OrderStatusPlus> = {
  UNPAID: {
    desc: '待支付',
    isClosed: false,
  },
  PAID: {
    desc: '待发货',
    isClosed: false,
  },
  BUYER_CLOSED: {
    desc: '已关闭',
    isClosed: true,
  },
  SYSTEM_CLOSED: {
    desc: '已关闭',
    isClosed: true,
  },
  SELLER_CLOSED: {
    desc: '已关闭',
    isClosed: true,
  },
  TEAMING: {
    desc: '拼团中',
    isClosed: false,
  },
  TEAM_FAIL: {
    desc: '拼团失败',
    isClosed: true,
  },
}
